Starting your journey with R

How to set up your R projects

Author

Sarah Zeller

Use .Rproj

Please use an .Rproj for every project you do. Save all your related files in this project. I will only accept submissions that are .Rproj.

Use here

Illustration by Allison Horst

When referring to any file – data, script, report – please always use the package here. You start in your .Rproj and then refer to paths like so:

library(here)
data <- here("data/my_data.csv") |> read.csv()

Use renv

When you want to share your project, the other person needs to download all the needed libraries. renv helps with that.

To use:

renv::activate()
renv::snapshot()

To recover the libraries:

renv::hydrate()

Use version control with github

  • use gitmojis, if you want to
  • extensively use .gitignore for sensitive data and output

Download files explicitly – two options

  1. Download files the following way:

library(here)

download.file(
  url, 
  here("output/file.csv")
)
  1. Describe how you downloaded files and where you saved them in your README:

I downloaded data 
from [url] and saved it 
to [output/some_file.csv].

Use dotenv

Never hardcode your credentials. If you need credentials to download things, use dotenv. With this, you create a file called .env in your root folder. The contents look like this:

password="1234"

Then, when you need to use the password:

library(dotenv)

some_function(password = Sys.getenv("password"))

Use a README

In your root folder, you should have a file called README.md. In this, you describe what you are doing, and how to replicate it.

Please also describe how to replicate your analysis. One way of doing this is including a chunk like this:


library(here)
here("R/load/01-load-first-data.R") |> source()
here("R/load/02-load-second-data.R") |> source()
...

Make sure your code runs

I need to run your code in the end. Please make sure your code runs, for example by simply copying the files you’re giving to me into a new Project and re-running them.

Folder structure

A folder structure I would strongly recommend is the following. Please make sure to seperate loading, wrangling, analysing data, and then reporting the results.

  • input
  • output
  • R
    • load
    • wrangle
    • analysis
  • report

Don’t save your workspace image

Structure your code

  • load librarys in the beginning
  • use a linter (mark everything with Ctrl + A, then lint with Ctrl + Shift + A)

Report with quarto

  • using a .qmd file to report your findings is handy
  • you can export to html, but also as a pdf or docx